<?php
namespace app\appadmin\controller\v1;
use app\app\Appbase;

//
//潜在项目
//
class Project extends Appbase 
{
	
	/**
	* 潜在项目
	* /appadmin/v1/project/index
	* 是否需要登录 
	* @is_need_login true
	*/
	public function index($shop_id=-1,$uid='',$staff_id='',$name=''){
		$shopId = $shop_id == -1 ? -1 : ( $shop_id =='' ? request()->shopId : $shop_id);
		if( empty($staff_id) && $staff_id = -1) $staff_id = request()->userId;
		try {  
			$model = model('ShopQzproj');
			$model = $model->with(['project','shopInfo']);
			if( !empty($uid) ) $model = $model->where('uid',$uid);
			if( !empty($staff_id) ) $model = $model->where('add_id',$staff_id);
			if( $shopId!=-1 ) $model = $model->where('shop_id',$shopId);
			if($name){
				$model = $model->where(function($query) use($name) {
					$query->where('user_name','like',"%$name%");
					$query->whereor('phone','like',"%$name%");
					$query->whereor('add_user','like',"%$name%");
				});
			}
			$model = $model->order('add_time','desc');
			$model = $model->page(input('pageIndex') ?: 1,input('pageSize') ?: 20);
			$list = $model->select();	
			return $this->ReSucess($list);	
		} catch (\Exception $e) {
			return $this->ReError($e->getMessage());	
		}
	}
	
	/**
	* 新增潜在项目
	* /appadmin/v1/project/create
	* 是否需要登录 
	* @is_need_login true
	* @method post
	*/
	public function create(){
		try {  
			$param = request()->post();
			$param['shop_id']  =request()->shopId;
			$param['add_id']  =request()->userId;
			$param['add_user']  =request()->userName;
			$res = model('ShopQzproj')->save($param);
			return $this->ReSucess($res);	
		} catch (\Exception $e) {
			return $this->ReError($e->getMessage());	
		}
	}
	
	/**
	* 潜在项目-分析
	* /appadmin/v1/project/analysis
	* 是否需要登录 
	* @is_need_login true
	*/
	public function analysis($shop_id=-1){
		try {  
			$model = model('ShopQzproj');
			$model = $model->with(['project','shopInfo']);
			$model = $model->where('shop_id',$shop_id);
			$model = $model->field('id,proj_id,count(*) as num');
			$model = $model->group('proj_id');
			$model = $model->page(input('pageIndex') ?: 1,input('pageSize') ?: 20);
			$list = $model->select();	
			return $this->ReSucess($list);	
		} catch (\Exception $e) {
			return $this->ReError($e->getMessage());	
		}
	}
	
	/**
	* 潜在项目-责任人分析
	* /appadmin/v1/project/person
	* 是否需要登录 
	* @is_need_login true
	*/
	public function person($shop_id=-1,$staff_id=-1){
		try {  
			$model = model('ShopQzproj');
			$model = $model->where('shop_id',$shop_id);
			if($staff_id!=-1) $model = $model->where('add_id',$staff_id);
			$model = $model->field('add_id,add_user,count(*) as num');
			$model = $model->group('add_id');
			$model = $model->page(input('pageIndex') ?: 1,input('pageSize') ?: 20);
			$list = $model->select();	
			return $this->ReSucess($list);	
		} catch (\Exception $e) {
			return $this->ReError($e->getMessage());	
		}
	}
	
	/**
	* 潜在项目-意向度分析
	* /appadmin/v1/project/intention
	* 是否需要登录 
	* @is_need_login true
	*/
	public function intention($shop_id=-1,$staff_id=-1){
		try {  
			$model = model('ShopQzproj');
			$model = $model->where('shop_id',$shop_id);
			if($staff_id!=-1) $model = $model->where('add_id',$staff_id);
			$model = $model->field('type,count(*) as num');
			$model = $model->group('type');
			$model = $model->page(input('pageIndex') ?: 1,input('pageSize') ?: 20);
			$list = $model->select();	
			return $this->ReSucess($list);	
		} catch (\Exception $e) {
			return $this->ReError($e->getMessage());	
		}
	}
	
	
	/**
	* 潜在项目-处理时间分析
	* /appadmin/v1/project/dates
	* 是否需要登录 
	* @is_need_login true
	*/
	public function dates($shop_id=-1,$year=''){
		try {  
			$model = model('ShopQzproj');
			$model = $model->where('shop_id',$shop_id);
			if( !empty($year) ) $model = $model->where('consume_time','like',"%$year%");
			$model = $model->field("count(*) as num");
			$model = $model->field("concat(month(consume_time),'月') month");
			$model = $model->group('month');
			$model = $model->page(input('pageIndex') ?: 1,input('pageSize') ?: 20);
			$list = $model->select();	
			return $this->ReSucess($list);	
		} catch (\Exception $e) {
			return $this->ReError($e->getMessage());	
		}
	}
	
}



